<template>
  <accDecide :isShow="isOverlayShow" :douyinId="douyinId"></accDecide>
  <NavBar :title="item.title"></NavBar>

  <div style="padding:30rpx 30rpx 0">
    <toSession v-if="isCustomAi" ref="toSessionRef" :item="item" @startAI="startAI"></toSession>
    <localSession v-else ref="localSessionRef" :item="item"></localSession>
  </div>
  <div style="height: 170rpx;">
    <div class="footer">
      <van-button round color="#83CD88" @click="startAI">开始AI生成</van-button>
    </div>
  </div>
  <van-notify id="van-notify"></van-notify>
  <van-toast id="van-toast"></van-toast>
</template>
<script setup>
import Toast from '../../../node_modules/@vant/weapp/dist/toast/toast';
import localSession from './child/localSession.vue'
import toSession from './child/toSession.vue'
import accDecide from './child/accDecide.vue'
import { useUserStore } from '@/store/modules';
import { onLoad } from '@dcloudio/uni-app';
import { computed, ref } from 'vue'
const app = getApp();
const item = ref({})
const userStore = useUserStore();
const localSessionRef = ref(null)
const toSessionRef = ref(null)
const isOverlayShow = ref(false)
const douyinId = ref('')
const isNull = ref(true)
const white = ['百问百答', '涨粉文案', '账号诊断', '招新人']
let isCustomAi = ref(null);
onLoad((e) => {
  item.value = JSON.parse(e.item)
  isCustomAi.value = white.includes(item.value.title);
})
let userInfo = computed(() => {
  return userStore.userInfo;
});
const dataDecide = () => {

  const id = item.value.id;
  let userFromStr = ''; // 普通功能用户ai
  app.globalData._query = {}
  if (id === 32 || id === 33 || id === 34 || id === 35) {
    if (id !== 33) {
      if (!toSessionRef.value.userInput) {
        isNull.value = false
      } else {
        isNull.value = true
      }
    }
    app.globalData._query.id = toSessionRef.value.query.map(item => item.id).join('|')
    app.globalData._query.text = toSessionRef.value.sessionContent
    if (id == 33) {
      app.globalData._query.question = `给${toSessionRef.value.sessionContent['想要的内容']}好评`
    }
    app.globalData._query.type = {
      id: id,
      title: item.value.title
    }
    if (toSessionRef.value.userInput) {
      app.globalData._query.question = toSessionRef.value.userInput; // 用户输入的问题
    }
    if (id == 34) { // 账号诊断
      app.globalData._query.question = `诊断账号:${toSessionRef.value.douyinId}`;
      if (!toSessionRef.value.douyinId) {
        isNull.value = false
      } else {
        isNull.value = true
      }
      // app.globalData._query = {
      //   id: toSessionRef.value.query.map(item => item.id).join('|'),
      //   question: `诊断账号:${toSessionRef.value.douyinId}`,
      //   type: {
      //     id: id
      //   }
      // }

    }
    return
  } else if (id === 31) {
    app.globalData._query.prompt = `取10个${localSessionRef.value.query.content}类型的抖音中文账号名，每一条账号名字数在5字以上10字以内`
  } else { // 普通内置功能
    const isShow = ref([])
    for (let key in localSessionRef.value.sessionContent) {
      if (!localSessionRef.value.sessionContent[key]) {
        isShow.value.push(false)
      } else {
        isShow.value.push(true)
      }
      userFromStr = userFromStr + `${key}:${localSessionRef.value.sessionContent[key]};`;
    }
    if (isShow.value.indexOf(false) >= 0) {
      isNull.value = false
    } else {
      isNull.value = true
    }
  }
  app.globalData._query.data = localSessionRef.value.query
  app.globalData._query.text = userFromStr || localSessionRef.value.sessionContent


  app.globalData._query.type = {
    id: id,
    type: 'function',
    title: item.value.title
  }
}

const judgementIsFalse = () => {
  const isShow = ref([])
  for (let key in localSessionRef.value.sessionContent) {
    if (!localSessionRef.value.sessionContent[key]) {
      isShow.value.push(false)
    } else {
      isShow.value.push(true)
    }
  }
  console.log(isShow.value, '难男男女女难男男女女难男男女女男男女女男男女女男男女女');
  if (isShow.value.indexOf(false) >= 0) {
    isNull.value = false
  } else {
    isNull.value = true
  }
}

const startAI = async () => {
  if (!userStore.userInfo.isCanUseAi && !userInfo.value.freeCount) {
    Toast({
      message: '您还不是付费会员',
      onClose: () => {
        uni.navigateTo({
          url: '/user-center/vip/index'
        });
      }
    });
    return;
  }
  dataDecide()
  if (!isNull.value) {
    return Toast.fail('请完善所有信息')
  }
  if (item.value.id === 34) {
    if (!toSessionRef.value.douyinId) {
      return Toast.fail('账号id不能为空')
    }
    douyinId.value = toSessionRef.value.douyinId
    isOverlayShow.value = true


    await new Promise(resolve => {
      return setTimeout(() => {
        isOverlayShow.value = false
        resolve()
      }, 8000)
    })
  }
  if ([31, 32, 33, 35].includes(item.value.id)) {
    return uni.switchTab({
      url: '/pages/chat/index'
    })
  }
  uni.navigateTo({
    url: '/pages/detailSession/index'
  })
}
</script>
<style lang="less" scoped>
  ::v-deep {
    .van-notify {
      background-color: var(--a-main-color) !important;
    }

    .footer .van-button {
      width: 600rpx;
      height: 86rpx;
      background-color: var(--a-main-color) !important;
    }
  }



  .footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    height: 170rpx;
    background: var(--a-main-tabNav-bg-color);
    text-align: center;
    line-height: 170rpx;
    box-shadow: 0rpx -4rpx 30rpx 0rpx rgba(0, 0, 0, 0.08);
  }
</style>